﻿using System;
using Game.TrapScripts;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Game.Platforms
{
    class DisappearingPlatform : IPlatform
    {
        private Texture2D _texture;

        private float _timeLeft = 2f;
        private bool _activated = false;

        public void Spawn(PlatformType type, Texture2D texture, int x, int y)
        {
            Type = type;
            _texture = texture;
            Position = new Vector2(x * Game1.GridSize, y * Game1.GridSize);
            BoundingRect = new Rectangle(x * Game1.GridSize, y * Game1.GridSize, texture.Width, texture.Height);
        }

        public void Spawn(PlatformType type, Texture2D texture, Vector2 pos)
        {
            Type = type;
            _texture = texture;
            Position = pos;
            BoundingRect = new Rectangle((int) pos.X,(int) pos.Y, texture.Width, texture.Height);
        }

        public void Update(GameTime gameTime)
        {
            
            if (_activated)
            {
                var elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
                _timeLeft -= elapsed;

                if (_timeLeft <= 0f)
                {
                    BoundingRect = Rectangle.Empty;
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (_timeLeft > 0)
                spriteBatch.Draw(_texture, Position, Color.White*(_timeLeft/2f));
        }

        public void Activate()
        {
            _activated = true;
        }

        public Vector2 Position { get; private set; }
        public PlatformType Type { get; private set; }
        public Rectangle BoundingRect { get; private set; }
    }
}
